package com.shwread.android.qysw.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.NewActionHelp;
import com.google.gson.reflect.TypeToken;
import com.shwread.android.qysw10000197.R;
import com.shwread.android.ui.page.chat.adapter.AreaAdapterForDB;
import com.shwread.greendao.gen.GreendaoDBManager;
import com.shwread.http.base.ObjectHttpResponseHandler;

import org.apache.http.Header;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import logic.bean.AreaBean2;
import logic.bean.AreaBeanModle;

/**
 * Created by lexy on 2016/10/12.
 */

public class AreaSelectActivity extends BaseBusinessActivity {

    private TextView tvTitle;
    private Button btnBack;
    private ListView lVCitylist;
    private int dataType = -1;
    private String name = "";
    private int id_p = -1;
    private int id_c = -1;
    private int id_a = -1;
    private static final int PROGRESS_SHOW = 111;
    private static final int PROGRESS_HIDE = 112;
    private ProgressDialog progressDialog;

    @Override
    public Handler getUIHandler() {
        return null;
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case PROGRESS_SHOW:
                    progressDialog = ProgressDialog.show(AreaSelectActivity.this, "", "加载中...");
                    break;
                case PROGRESS_HIDE:
                    progressDialog.dismiss();
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.city_select_layout);
        tvTitle = (TextView) findViewById(R.id.common_header_name);
        btnBack = (Button) findViewById(R.id.common_header_back);
        btnBack.setVisibility(View.VISIBLE);
        lVCitylist = (ListView) findViewById(R.id.lVCitylist);

        name = getIntent().getStringExtra("name");
        id_p = getIntent().getIntExtra("id_p", -1);
        id_c = getIntent().getIntExtra("id_c", -1);
        id_a = getIntent().getIntExtra("id_a", -1);
        dataType = getIntent().getIntExtra("dataType", -1);


        tvTitle.setText(name);
        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AreaSelectActivity.this.finish();
            }
        });
        lVCitylist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Object obj = parent.getItemAtPosition(position);
                AreaBeanModle areaBeanModle = (AreaBeanModle) obj;
                Intent intent = null;
                if (dataType == 4) {
                    intent = new Intent(AreaSelectActivity.this, ContactsActivity.class);
                    intent.putExtra("name", areaBeanModle.getCompanyName());
                    intent.putExtra("companyId", areaBeanModle.getCompanyId());
                } else {
                    intent = new Intent(AreaSelectActivity.this, AreaSelectActivity.class);
                    if (dataType == 2) {
                        intent.putExtra("name", areaBeanModle.getCityName());
                    } else if (dataType == 3) {
                        intent.putExtra("name", areaBeanModle.getAreaName());
                    }
                    intent.putExtra("id_p", areaBeanModle.getProvinceId());
                    intent.putExtra("id_c", areaBeanModle.getCityId());
                    intent.putExtra("id_a", areaBeanModle.getAreaId());
                    intent.putExtra("dataType", areaBeanModle.getDataType());
                }
                startActivity(intent);
            }
        });
        handler.sendEmptyMessage(PROGRESS_SHOW);
        NewActionHelp.qryEnterpriseLevelAreaList(this, id_p, id_c, id_a, new QryEnterpriseLevelAreaList());
    }

    public class QryEnterpriseLevelAreaList
            extends
            ObjectHttpResponseHandler<AreaBean2> {

        @Override
        public void onFailure(int i, Header[] headers, String s, Throwable throwable) {
            android.util.Log.e("通讯录改造失败2", s);
            List<AreaBeanModle> list = GreendaoDBManager.queryAreaList("n");
            lVCitylist.setAdapter(new AreaAdapterForDB(AreaSelectActivity.this, list, dataType));
            handler.sendEmptyMessage(PROGRESS_HIDE);
        }

        @Override
        public void onSuccess(int code, Header[] headers, AreaBean2 data, String s) {
            android.util.Log.e("通讯录改造失败2", data.toString() + "\n" + s);
            dataType = data.getDataType();
            List<AreaBeanModle> list = new ArrayList<>();
            for (int i = 0; i < data.getAreaList().size(); i++) {
                if (data.getAreaList().get(i).getIsDelete().equals("n")) {
                    list.add(data.getAreaList().get(i));
                }
            }
            lVCitylist.setAdapter(new AreaAdapterForDB(AreaSelectActivity.this, list, dataType));
            GreendaoDBManager.deleteArea(dataType);
            GreendaoDBManager.insertAreaList(data.getAreaList());
            handler.sendEmptyMessage(PROGRESS_HIDE);
        }

        @Override
        public Type getType() {
            return new TypeToken<AreaBean2>() {
            }.getType();
        }
    }
}
